package com.sina.shop.dao.foot;

import com.sina.shop.dao.ShopDataSourceDao;
import com.sina.shop.domain.foot.Foot;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-05-14:56
 */
@Repository("footDao")
public class FootDaoImpl extends ShopDataSourceDao implements FootDao {

    @Override
    public List<Foot> getFootByAdminId(Long adminId) {
        String hql = "from Foot f where f.admin.id= :adminId order by f.createTime desc";
        return list(hql, new String[]{"adminId"}, new Object[]{adminId}, -1, -1);
    }

    @Override
    public Integer deleteFoot(Long id) {
        String hql = "delete from Foot f where f.id = :id";
        return template.executeWithNativeSession(session -> {
            Query query = session.createQuery(hql);
            query.setParameter("id", id);

            return query.executeUpdate();
        });
    }

    @Override
    public Foot getFootByAidAndGid(Long adminId, Long goodsId) {
        String hql = "from Foot f where f.admin.id=:adminId and f.goods.id=:goodsId";
        return unique(hql, new String[]{"adminId", "goodsId"}, new Object[]{adminId, goodsId});
    }
}
